from django.test import TestCase
from personModel.models import person
from django.http import HttpResponse
import json
from util import myutils
# Create your tests here.
def save(request):
    qd = request.POST
    id = qd.get('id')
    if None == id:
        p = person(name=request.POST['name'] , gender = request.POST['gender'], age=request.POST['age'])
    else:
        p = person.objects.get(id=id)
        p.name = request.POST['name']
        p.gender = request.POST['gender']
        p.age = request.POST['age']

    p.save()
    str = json.dumps(myutils.model2json(p))
    print(str)
    if None == id:
        return myutils.retOKJson("插入成功！",str)
    else:
        return myutils.retOKJson("修改成功" , str)

def query(request):
    list = person.objects.all()
    print(list)
    for p in list:
        if p.gender == 1:
            p.gender = '男'
        elif p.gender == 2:
            p.gender = '女'
        else:
            p.gender = ''
    str = json.dumps(myutils.model2jsonArr(list))
    return myutils.retOKJson("列表查询成功！",str)

def delete(request):
    p = person.objects.get(id = request.POST['id'])
    p.delete()
    str = json.dumps(myutils.model2json(p))
    return myutils.retOKJson("删除成功！", str)